#include <DataStruct.h>
#include <QDebug>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cstdlib>
#include <windows.h>

#define random(x) rand() % (x);
#define PI 3.1415926
/*
 * 计算向量模长
 * 参数x, y分别为向量横纵坐标
*/
double pve_Mod(double x, double y) {
    return sqrt(x * x + y * y);
}

double angleConvert(double angle) {
    return -angle - 90;
}

/*
 * 预测求位置，判断是否需要开盾
 * 参数: data: 战场数据
*/

bool pve_shield(DataStruct *data) {
    double dis = 3000.0;
    int min_index = 0;
    for (int i = 0; i < data -> ball_size; ++i) {
        double ball_x = data->ball[i].x + data->ball[i].v_x * 0.01;
        double ball_y = data->ball[i].y + data->ball[i].v_y * 0.01;
        //if (i == data->ball_size - 1)
            //qDebug() << data->ball[i].x << ' ' << data->ball[i].v_x << endl;
        double pre_dis = pve_Mod(ball_x - data->plane1.x, ball_y - data->plane1.y);
        if (pre_dis < dis) {
            dis = pre_dis;
            min_index = i;
        }
    }
    if (dis <= data->plane1.r + data->ball[min_index].r + 10)
        return true;
    return  false;
}

void pve_aim_ball(DataStruct *data, BallStruct &ball, KeyStruct *key) {
    //qDebug() << "吱吱\n";
    double px = cos(angleConvert(data->plane1.angle) * PI / 180), py = sin(angleConvert(data->plane1.angle) * PI / 180);
    //qDebug() << py << tan(-data->plane1.angle - 90)<< endl;
    //exit(0);
    double pbx = ball.x - data->plane1.x, pby = ball.y - data->plane1.y;
    double cross = px * pby - py * pbx;
    qDebug() << cross;
    if (cross < 0) {
        key->rotate_left = true;
        key->rotate_right = false;
    }
    else {
        key->rotate_left = false;
        key->rotate_right = true;
    }
}

/*
 * 控制飞机行动
 * 参数: data: 战场数据, key: 飞机动作
*/
void ai_pve(DataStruct *data, KeyStruct *key) {
    /*static int time = 0;
    time++;
    if (time % 10 == 0)
        qDebug() << time << endl;*/
    //qDebug() << data->plane1.x << endl;
    //qDebug() << -data->plane1.angle - 90 << endl;
    //qDebug() << key << endl;

    //初始化飞机动作
    key->forward = false;
    key->rotate_left = false;
    key->rotate_right = false;
    key->shoot = true;
    key->shield = false;
    if (data->plane1.angle < -180)
        data->plane1.angle += 360;
    else if (data->plane1.angle > 180)
        data->plane1.angle -= 360;

    //判断是否要开盾
    if (pve_shield(data))
        key->shield = true;


    //如果球个数不为零
    //9.7 22:51 update1: 以血量，距离排序后确定攻击目标
    if (data->ball_size)
    {
        int shoot_id = 0;   //初始化攻击目标
        int max_hp = 3;
        for (int i = 0; i < data->ball_size; ++i) {
            max_hp = std::max(max_hp, data->ball[i].hp);
            if (data->plane1.x < data->ball[i].x && data->ball[i].v_x > 0 && data->ball[i].v_y < 0)
                continue;
            if (data->plane1.x > data->ball[i].x && data->ball[i].v_x < 0 && data->ball[i].v_y < 0)
                continue;
            if (data->ball[i].hp > data->ball[shoot_id].hp)
                shoot_id = i;
            else if (data->ball[i].hp == data->ball[shoot_id].hp) {
                double dis_i = pve_Mod(data->plane1.x - data->ball[i].x, data->plane1.y - data->ball[i].y);
                double dis_shoot_id = pve_Mod(data->plane1.x - data->ball[shoot_id].x, data->plane1.y - data->ball[shoot_id].y);
                if (dis_i < dis_shoot_id)
                    shoot_id = i;
            }
        }
        //如果都是小球
        if (max_hp <= 2) {
            shoot_id = random(data->ball_size);
        }
        //瞄准优先级最高目标
        pve_aim_ball(data, data->ball[shoot_id], key);
    }

}
